package com.atguigu.spzx.manager.controller.system;


import com.atguigu.spzx.manager.service.system.SysUserService;
import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.entity.system.AuthContextUtil;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.util.SpzxAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录验证
     * @param loginDto
     * @return
     */
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto){
       LoginVo loginVo =  sysUserService.login(loginDto);
        SpzxAssert.notNull(loginVo,"登录失败");
        return Result.ok(loginVo);
    }

    /**
     * 获取用户信息 直接从ThreadLocal中获取用户信息
     * @param
     * @return
     */

 /**  @GetMapping("/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token){
       SysUser sysUser = sysUserService.getUserInfo(token);
       return Result.ok(sysUser);
    }
  */
    @GetMapping("/userinfo")
    public Result<SysUser> getUserInfo(){
        return Result.ok(AuthContextUtil.getUser());
    }

    /**
     * 退出登录
     * @param token
     * @return
     */
    @GetMapping("/logout")
    public Result logout(@RequestHeader(value = "token") String token){
        sysUserService.logout(token);
        return Result.ok();
    }

    /**
     * 图片验证码
     * @return
     */
    @GetMapping("/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode(){
        ValidateCodeVo validateCodeVo = sysUserService.generateValidateCode();
        return Result.ok(validateCodeVo);
    }

}
